﻿// ***********************************************************************
// Assembly         : 2 BestQA.CommandSubscribe
// Author           : Administrator
// Created          : 06-20-2015
//
// Last Modified By : Administrator
// Last Modified On : 06-22-2015
// ***********************************************************************
// <copyright file="CreateQuestionCmdHandler.cs" company="Microsoft">
//     Copyright (c) Microsoft. All rights reserved.
// </copyright>
// <summary>创建问题命令处理</summary>
// ***********************************************************************

using BestQA.Commands;
using BestQA.Domain.Answers;
using BestQA.Domain.Questions;
using ECommon.Components;
using ECommon.Logging;
using ENode.Commanding;

namespace BestQA.CommandSubscribe.CommandHandlers
{
    [Component]
    class QuestionCmdHandler :
        ICommandHandler<CreateQuestionCmd>,
        ICommandHandler<ApplyNewAnswerCmd>,
        ICommandHandler<QuestionVoteUpCmd>
    {
        private ILogger _logger;

        public QuestionCmdHandler(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.Create(GetType().Name);
        }

        public void Handle(ICommandContext context, CreateQuestionCmd command)
        {
            context.Add(new Question(
                command.AggregateRootId,
                command.Title, 
                command.Content, 
                command.Reward,
                command.Tag, 
                command.UserId));
            _logger.InfoFormat("Handled {0}, Question Title:{1}", typeof(CreateQuestionCmd).Name, command.Title);
        }

        public void Handle(ICommandContext context, ApplyNewAnswerCmd command)
        {
            _logger.InfoFormat("Handled {0}, Question AnswerId:{1}", typeof(ApplyNewAnswerCmd).Name, command.AnswerId);
            context.Get<Question>(command.AggregateRootId).NewAnswer(context.Get<Answer>(command.AnswerId));
        }

        public void Handle(ICommandContext context, QuestionVoteUpCmd command)
        {
            _logger.InfoFormat("Handled {0}, Question Id:{1}", typeof(QuestionVoteUpCmd).Name, command.AggregateRootId);
            context.Get<Question>(command.AggregateRootId).Support(command.UserId);
        }
    }
}
